﻿#include "dialoglogin.h"
#include "qevent.h"
#include "ui_dialoglogin.h"

#include "AppConfig/appconfig.h"
#include <QJsonObject>
#include "Business/inputhandler.h"

#include "mainwindow.h"
#include <QEvent>

extern DialogLogin* login;
extern MainWindow* mainWindow;

DialogLogin::DialogLogin(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DialogLogin)
{
    ui->setupUi(this);

    setWindowIcon(QIcon(":/icon/program/api.svg"));
    // 设置父窗口关闭时，此窗口也关闭
    this->setAttribute(Qt::WA_QuitOnClose, false);


    // 初始化ip选项
    AppConfig& config = AppConfig::getInstance();
    // 添加之前使用过的ip地址
    ui->comboBoxIPAddress->addItem(config.globalValue("lastIP").c_str());
    // 更新一下“编辑目标ip”的插入的位置
    IPAddIndex ++;
    // 添加“编辑目标ip”
    ui->comboBoxIPAddress->addItem("编辑目标ip");
    ui->comboBoxIPAddress->setCurrentIndex(0);
    // 设置不可编辑模式
    ui->comboBoxIPAddress->setEditable(false);
    ui->lineEditDBHost->setText(config.globalValue("SQLHostName").c_str());
    ui->lineEditDBPort->setText(config.globalValue("SQLPort").c_str());
    ui->lineEditDBUserName->setText(config.globalValue("SQLUserName").c_str());
    ui->lineEditDBUserPwd->setText(config.globalValue("SQLUserPwd").c_str());
    ui->lineEditDBUserPwd->setEchoMode(QLineEdit::Password);
}

DialogLogin::~DialogLogin()
{
    delete ui;
}

void DialogLogin::closeEvent(QCloseEvent *event)
{
    QJsonObject in, out;
    InputHandler::getInstance()["MainWindow::closeEvent"](in, out);

    emit closeProgram();
    event->accept();
}

void DialogLogin::on_comboBoxIPAddress_currentIndexChanged(int index)
{
    // 如果当前的选项选择了"编辑目标ip"，则可以编辑当前的值，如果选择了其他已经存在选项，则无法编辑。
    if (index == IPAddIndex) {
        ui->comboBoxIPAddress->setEditable(true);
    } else {
        ui->comboBoxIPAddress->setEditable(false);
    }
}

void DialogLogin::on_btnOK_clicked()
{
    // 如果当前点击确定了则会与服务器连接来确定身份
    QString IP = ui->comboBoxIPAddress->currentText();
    QString account = ui->lineEditAccount->text();
    QString password = ui->lineEditPassword->text();

    QString DBHost = ui->lineEditDBHost->text();
    QString DBPort = ui->lineEditDBPort->text();
    QString DBUserName = ui->lineEditDBUserName->text();
    QString DBUserPwd = ui->lineEditDBUserPwd->text();

    QJsonObject json_in, json_out;
    json_in.insert("account", account);
    json_in.insert("password", password);
    json_in.insert("IP", IP);

    json_in.insert("DBHost", DBHost);
    json_in.insert("DBPort", DBPort);
    json_in.insert("DBUserName", DBUserName);
    json_in.insert("DBUserPwd", DBUserPwd);

    InputHandler::getInstance()["DialogLogin::on_buttonBox_accepted"](json_in, json_out);
    if (json_out["status"] == "true") {
        // 验证成功
        this->hide();

        mainWindow->init();
        mainWindow->show();

    } else {
        // 验证失败
        ui->labelShowLoginMsg->setText("login failed");
    }
}


void DialogLogin::on_btnCancel_clicked()
{
    qDebug() << "关闭程序";
    emit closeProgram();
}

